﻿using System;
using System.Collections.Generic;
using Qadir.Collections.Generic;
using System.Net;

namespace Qadir.Net
{
    /// <summary>
    /// An wrapper around classes within the System.Net namespace that supports cookies, among other now-common Web features.
    /// </summary>
    public partial class Internet
    {
        /// <summary>
        /// Any and all HTTP cookies carried in this instance. Especially good for automated HTTP requests.
        /// </summary>
        public Manager<Cookie> Cookies { get; set; }

        /// <summary>
        /// A set of Requestor objects streamlining requests and responses.
        /// </summary>
        public Manager<Requestor> Requestors { get; set; }

        public Internet ()
        {

        }

        /// <summary>
        /// Opens a request to a resource on the Internet.
        /// </summary>
        /// <param name="url">The location of the resource.</param>
        /// <param name="method">The HTTP request method to use.</param>
        /// <returns>Returns a Requestor instance ready to fulfill its bounden duty.</returns>
        public Requestor Request(string url, string method = WebRequestMethods.Http.Get)
        {
            Requestor result = Requestor.Create(url, method);
            Requestors.Add(result);
            return result;
        }

        /// <summary>
        /// Opens a POST request to a resource on the Internet.
        /// </summary>
        /// <param name="url">The location of the resource.</param>
        /// <param name="values">Any POST data to be written to the request</param>
        /// <returns>Returns a Requestor instance ready to fulfill its bounden duty.</returns>
        public Requestor RequestPost(string url, IDictionary<string, string> values)
        {
            Requestor result = Requestor.CreatePost(url, values);
            Requestors.Add(result);
            return result;
        }

        /// <summary>
        /// Requests a resource on the Internet and returns a response.
        /// </summary>
        /// <param name="url">The location of the resource to access.</param>
        /// <param name="method">The HTTP request method to use for this request.</param>
        /// <param name="asynchronous">A boolean value indicating whether to perform the operation on a new thread.</param>
        /// <returns>Returns the response of the server hosting the desired resource.</returns>
        public static Responder Surf(string url, string method = WebRequestMethods.Http.Get, bool asynchronous = true)
        {
            return Requestor.Create(url, method).GetResponder(asynchronous);
        }

        /// <summary>
        /// Requests a resource on the Internet and returns a response.
        /// </summary>
        /// <param name="url">The location of the resource to access.</param>
        /// <param name="method">The HTTP request method to use for this request.</param>
        /// <param name="asynchronous">A boolean value indicating whether to perform the operation on a new thread.</param>
        /// <returns>Returns the response of the server hosting the desired resource.</returns>
        public static byte[] SurfAsBytes(string url, string method = WebRequestMethods.Http.Get, bool asynchronous = true)
        {
            return Requestor.Create(url, method).GetResponder(asynchronous).ResponseBytes;
        }

        /// <summary>
        /// Requests a resource on the Internet and returns a response.
        /// </summary>
        /// <param name="url">The location of the resource to access.</param>
        /// <param name="method">The HTTP request method to use for this request.</param>
        /// <param name="asynchronous">A boolean value indicating whether to perform the operation on a new thread.</param>
        /// <param name="blocking">A boolean value indicating whether the calling thread should block until operation completion.</param>
        /// <returns>Returns the response of the server hosting the desired resource.</returns>
        public static HttpWebResponse SurfAsResponse(string url, string method = WebRequestMethods.Http.Get, bool asynchronous = true, bool blocking = true)
        {
            return Requestor.Create(url, method).GetResponse(asynchronous, blocking);
        }

        /// <summary>
        /// Requests a resource on the Internet and returns a response.
        /// </summary>
        /// <param name="url">The location of the resource to access.</param>
        /// <param name="method">The HTTP request method to use for this request.</param>
        /// <param name="asynchronous">A boolean value indicating whether to perform the operation on a new thread.</param>
        /// <returns>Returns the response of the server hosting the desired resource.</returns>
        public static string SurfAsText(string url, string method  = WebRequestMethods.Http.Get, bool asynchronous = true)
        {
            return Requestor.Create(url, method).GetResponder(asynchronous).ResponseText;
        }
    }
}
